package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;


    /**
     api/cart/getCartCheckedList/{userId}
     * 获取选中状态的购物车列表
     * 接口路径：GET/api/cart/getCartCheckedList/{userId}
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){


        return cartService.getCartCheckedList(userId);
    }


    /**
     删除购物车
     接口路径：DELETE/api/cart/deleteCart/{skuId}

     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable String skuId,HttpServletRequest request){

        //获取用户
        String userId = AuthContextHolder.getUserId(request);
        //判断
        if (StringUtils.isEmpty(userId)) {

            userId = AuthContextHolder.getUserTempId(request);
        }

        //调用Service中的方法 实现删除购物车功能，删除购物车要根据用户的id和商品库存单元id删除
        cartService.deleteCart(userId,skuId);

        return Result.ok();
    }


    /**
     接口路径：GET/api/cart/checkCart/{skuId}/{isChecked}
     更新选中状态

     */
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){

        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //判断
        if (StringUtils.isEmpty(userId)) {

            userId=AuthContextHolder.getUserTempId(request);
        }

        //调用Service层方法实现更新选中状态的功能
        cartService.checkCart(userId,skuId,isChecked);

        return Result.ok();
    }


    /**
     * api/cart/cartList
     * 获取购物车列表
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //获取userId
        String userId = AuthContextHolder.getUserId(request);
        //获取userTempId
        String userTempId=AuthContextHolder.getUserTempId(request);

        //调用service
        List<CartInfo> cartInfoList=cartService.cartList(userId,userTempId);


        return Result.ok(cartInfoList);
    }



    /**
     * api/cart/addToCart/{skuId}/{skuNum}
     * 加入购物车

     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request){

        //获取userId
        String userId = AuthContextHolder.getUserId(request);
        //判断
        if (StringUtils.isEmpty(userId)) {

            userId = AuthContextHolder.getUserTempId(request);
        }

        //通用service
        cartService.addToCart(userId,skuId,skuNum);

        return Result.ok();
    }


}
